package exttable.testfiller;

import dbutils.DbUtils;
import exttable.testfiller.diseasetree.Desiase;
import utils.Localisation;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: Feb 22, 2010
 * Time: 12:58:21 AM
 * To change this template use File | Settings | File Templates.
 */
public class ProblemDescriptionPanel implements TreeSelectionListener {
    JList blockLevel;
    JList classLevel;
    JList categoryLevel;
    JList diseaseList;
    JTree jTree1;
    JFrame f;

    public ProblemDescriptionPanel() throws Exception {
        f = new JFrame(Localisation.getMessage("title.problemdiscr"));
        jTree1 = new JTree(processHierarcy(getDesiaseNodes()));
        jTree1.addTreeSelectionListener(this);
        f.add(jTree1);
        f.setVisible(true);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void valueChanged(TreeSelectionEvent event) {
//        System.out.println(jTree1.getLastSelectedPathComponent().toString());
    }

    private static DefaultMutableTreeNode processHierarcy(Desiase d) {
        DefaultMutableTreeNode node = new DefaultMutableTreeNode(d.getNameRu());
        DefaultMutableTreeNode child;
        for (Desiase res : d.getListDesiase()) {
            if (res.getListDesiase().size() > 0) {
                child = processHierarcy(res);
            } else {
                child = new DefaultMutableTreeNode(res.getNameRu());
            }
            node.add(child);
        }
        return node;
    }


    static Desiase getDesiaseNodes() throws Exception {
        Desiase toReturn = new Desiase();
        Connection c = DbUtils.getDBConnectionGeneral();
        PreparedStatement ps = c.prepareStatement("select id, disease_name from disease_tree where parent_id = 0");
        ResultSet rs = ps.executeQuery();
        List<Desiase> listNodes = getList(rs);
        toReturn.setListDesiase(listNodes);
        c.close();
        return toReturn;
    }

    static List<Desiase> getList(ResultSet rs) throws Exception {
        Connection c = DbUtils.getDBConnectionGeneral();
        PreparedStatement ps;
        List<Desiase> listNodes = new ArrayList<Desiase>();
        while (rs.next()) {
            Desiase d = new Desiase();
            d.setId(rs.getString(1));
            d.setNameRu(rs.getString(2));
            ps = c.prepareStatement("select id, disease_name from disease_tree where parent_id = ?");
            ps.setString(1, d.getId());
            ResultSet rs2 = ps.executeQuery();
            List<Desiase> listNodes2 = getList(rs2);
            d.setListDesiase(listNodes2);
            listNodes.add(d);
        }
        c.close();
        return listNodes;
    }

    public static void main(String[] args) throws Exception {
        new ProblemDescriptionPanel();
    }
}
